const mongoose = require('mongoose')

const schema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    unique: true,
    trim: true,
    lowercase: true
  },
  password: {
    type: String,
    required: true,
    set (val) {
      return require('bcryptjs').hashSync(val, 10)
    }
  },
  nickname: String,
  avatar: {
    type: String,
    default: 'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2974706256,1511610652&fm=11&gp=0.jpg'
  }
})

// schema.virtual('roles', {
//   ref: "Role",
//   localField: '_id',
//   foreignField: 'users',
//   justOne: false
// })

schema.set('toObject', { virtuals: true })
schema.set('toJSON', { virtuals: true })

module.exports = mongoose.model('User', schema)
